<?php

namespace app\yy\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\yy\model\Website as WebModel;
use app\yy\model\Archive as ArchiveModel;
use app\yy\model\Server as ServerModel;

class Website extends Admin
{
    public $type = [
        '0' => 'easyShop',
        '1' => 'openCart',
        '2' => 'Wordpress'
    ];

    public $status = [
        '0' => '停止:default',
        '1' => '待迁移:info',
        '2' => '迁移中:primary',
        '3' => '迁移失败:danger',
        '4' => '迁移成功:success'
    ];

    public function index()
    {
        // 获取查询条件
        $map = $this->getMap();
        // 数据列表
        $data_list = WebModel::alias('w')
            ->join('yy_admin_attachment a','w.atta_id=a.id','left')
            ->where($map)
            ->field('w.*,a.path,a.name,a.driver,a.ext')
            ->order('id desc')
            ->paginate();
        foreach ($data_list as $key => &$value) {
            if ($value['driver'] == 'local') {
                $path = PUBLIC_PATH . $value['path'];
            } else {
                $path = $value['path'];
            }
            if (is_file('.' . config('public_static_path') . 'admin/img/files/' . $value['ext'] . '.png')) {
                $value['download'] = '<a href="' . $path . '"
                        data-toggle="tooltip" title="点击下载">
                        <img class="image" src="' . config('public_static_path') . 'admin/img/files/' . $value['ext'] . '.png"></a>';
            } else {
                $value['download'] = '<a href="' . $path . '"
                        data-toggle="tooltip" title="点击下载">
                        <img class="image" src="' . config('public_static_path') . 'admin/img/files/file.png"></a>';
            }
        }
        $archive = ArchiveModel::column('title','id');
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('迁移网站') // 设置页面标题
            ->setTableName('website') // 设置数据表名
            ->setSearchArea([
                ['text', 'brand', 'YY-品牌名', 'like'],
                ['text', 'domain', '迁移网站', 'like'],
                ['select', 'type', '网站类型', '', '', $this->type],
                ['select', 'archive_id', '数据库', '', '', $archive],
                ['select', 'status', '状态', '', '', $this->status],
            ])
            ->addColumns([ // 批量添加列
                ['id', 'ID'],
                ['download', '文件'],
                ['brand', 'YY-品牌名'],
                ['archive_id', '数据库', '', '', $archive],
                ['db_database', '数据库名'],
                ['domain', '迁移网站'],
                ['type', '类型', '', '', $this->type],
                ['status', '状态', 'status', '', $this->status],
                ['remark', '备注'],
                ['start_time', '开始迁移时间'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete') // 批量添加顶部按钮
            ->addRightButtons(['enable'=>['title' => '启用'],'disable'=>['title'=>'停止'],'delete']) // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->setColumnWidth([
                '_checkbox' => 20,
                'id' => 25,
                'download' => 40,
                'brand' => 80,
                'archive_id' => 30,
                'db_database' => 25,
                'type' => 35,
                'status' => 55,
                'remark' => 55,
                'start_time' => 30,
                'right_button' => 35,
            ])
            ->fetch(); // 渲染页面
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data['brand']){
                $this->error('YY-品牌名 必填');
            }
            if (!$data['atta_id']){
                $this->error('YY文件 必填');
            }
            if (!$data['domain']){
                $this->error('迁移网站 必填');
            }
            if (!$data['server_id']){
                $this->error('服务器 必选');
            }
            if (!$data['archive_id']){
                $this->error('数据库 必选');
            }
            if (!$data['db_database']){
                $this->error('数据库名 必填');
            }
            $data['db_database'] = trim($data['db_database']);
            if (WebModel::create($data)) {
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }
        $server = ServerModel::column('name','id');
        $archive = ArchiveModel::where('status',1)->column('title','id');
        return ZBuilder::make('form')
            ->setPageTitle('添加')
            ->addSelect('type', '网站类型', '', $this->type,0)
            ->addSelect('server_id', '网站所属服务器', '', $server,1)
            ->addText('brand','YY-品牌名')
            ->addText('domain','迁移网站','二级域名【www.baidu.com】，与网站的目录需保持一致')
            ->addSelect('archive_id', '网站所属数据库', '', $archive,1)
            ->addText('db_database','数据库名')
            ->addFile('atta_id','YY文件','','','','xlsx')
            ->fetch();
    }

}